#include "head.h"
// user_t data[50] =
//     {
//         {.name = "刘备", .passwd = "1", .IDcard = 1,.value=100,.ticket="0"},
//         {.name = "关羽", .passwd = "2", .IDcard = 1,.value=100,.ticket="0"},
//         {.name = "张飞", .passwd = "3", .IDcard = 1,.value=100,.ticket="0"},
//         {.name = "诸葛", .passwd = "4", .IDcard = 1,.value=100,.ticket="0"},
//         {.name = "赵云", .passwd = "5", .IDcard = 1,.value=100,.ticket="0"},
//         {.name = "马超", .passwd = "6", .IDcard = 0,.value=100,.ticket="0"},
//         {.name = "祝融", .passwd = "7", .IDcard = 0,.value=100,.ticket="0"},
//         {.name = "小乔", .passwd = "8", .IDcard = 0,.value=100,.ticket="0"},
//         {.name = "大乔", .passwd = "9", .IDcard = 0,.value=100,.ticket="0"}};

// 单链表的创建
u_list *init_link_list(void)
{
    // 创建头结点的动态空间
    u_list *list = (u_list *)malloc(sizeof(u_list));
    if (NULL == list)
    {
        ERRLOG("单链表创建失败");
        return NULL;
    }
    list->next = NULL;
    return list;
}

// 单链表的销毁
void free_link_list(u_list **list)
{
    if (NULL == *list)
    {
        printf("链表 为 NULL\n");
        return;
    }

    // 1 销毁结点
    u_node *ptr_1 = (*list)->next;
    u_node *ptr_2 = ptr_1->next;
    for (size_t i = 0; ptr_1 != NULL; i++)
    {
        free(ptr_1);
        // if 判断  判断 ptr2 = NULL 的时候 我们如何处理
        ptr_1 = ptr_2;
        if (NULL != ptr_2)
        {
            ptr_2 = ptr_2->next;
        }
    }
    // 2 销毁头结点
    free(*list);
    *list = NULL;
}

// 单链表的遍历
void print_link_list(u_list *list)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return;
    }
    // 遍历
    u_node *ptr = list->next;
    for (size_t i = 0; ptr != NULL; i++)
    {
        printf("姓名:%s 密码:%s 会员等级:%d 账户余额:%d \n",
               ptr->data.name, ptr->data.passwd, ptr->data.IDcard,
               ptr->data.value);
        ptr = ptr->next;
    }
    if(ptr!=NULL)
    printf("姓名:%s 密码:%s 会员等级:%d 账户余额:%d \n",
               ptr->data.name, ptr->data.passwd, ptr->data.IDcard,
               ptr->data.value);
}

// 单链表的插入(任意位置插入)
void insert_node(u_list *list, user_t data, int index)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return;
    }
    u_node *new_node = (u_node *)malloc(sizeof(u_node));
    new_node->data = data;
    new_node->next = NULL;
    // 头插法
    if (1 == index)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }
    // 任意位置插入
    u_node *ptr = list->next;
    for (int i = 1; i < index - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }
    if (NULL == ptr)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;
    return;
}

// //单链表的删除
void delete_node(u_list *list, int pos)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return;
    }
    if (NULL == list->next)
    {
        printf("链表的内容为空\n");
        return;
    }
    u_node *ptr_1 = list->next;
    u_node *ptr_2 = NULL;
    if (1 == pos)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }
    // 指向要删除节点的前一个节点
    for (int i = 1; i < pos -1&& ptr_1 != NULL; i++)
    {
        ptr_1 = ptr_1->next;
    }
    if (NULL == ptr_1)
    {
        printf("ptr_1已经指向NULL\n");
        return;
    }
    
    // 指向要删除的节点 ptr_2
    ptr_2 = ptr_1->next;
    ptr_1->next = ptr_2->next;
    free(ptr_2);

    return;
}

// 单链表的查找
u_node *find_node_id(u_list *list, int pos)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("链表的内容为空\n");
        return NULL;
    }
    u_node *ptr = list->next;
    for (int i = 0; i < pos && NULL != ptr; i++)
    {
        ptr = ptr->next;
    }
    if (NULL == ptr)
    {
        printf("ptr已经指向NULL\n");
        return NULL;
    }
    return ptr;
}

// 单链表的查找
u_node *find_node_name(u_list *list, char *name)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("链表的内容为空\n");
        return NULL;
    }
    u_node *ptr = list->next;
    for (int i = 0; NULL != ptr; i++)
    {
        if (!strcmp(ptr->data.name, name))
        {
            return ptr;
        }
        ptr = ptr->next;
    }
    return NULL;
}

// 购买后修改单链表的内容
u_node *revise_node_name(u_list *list, char *name, user_t data)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("链表的内容为空\n");
        return NULL;
    }
    u_node *ptr = list->next;
    for (int i = 0; NULL != ptr; i++)
    {
        if (!strcmp(ptr->data.name, name))
        {
            ptr->data = data;
            return ptr;
        }
        ptr = ptr->next;
    }
    return NULL;
}

// 注册
void zc(u_list *list)
{
    system("clear");
    user_t data;
    printf("请输入你的账号:  ");
    scanf("%s", data.name);
    printf("请输入你的密码:  ");
    scanf("%s", data.passwd);
    data.IDcard = 2;
    data.value = 0;
    insert_node(list, data, 1);
    printf("注册成功！\n");
    scanf_file(2,list);
    getchar();
    getchar();
}



//单链表的合并
u_list *S_List_merge(u_list *list1, u_list *list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("链表不存在\n");
        return list1;
    }

    // 1、求链表list1 的链表长度
    u_node *ptr_1 = list1->next;
    int len = 0;

    while (ptr_1 != NULL)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    // 插入到链表中
    ptr_1 = list1->next;
    u_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        insert_node(list1, ptr_2->data, len);
        ptr_2 = ptr_2->next;
    }

    return list1;
}

//找到删除单链表的内容
u_node *find_del_name(u_list *list, char *name)
{
    if (NULL == list)
    {
        printf("链表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("链表的内容为空\n");
        return NULL;
    }
    u_node *ptr = list->next;
    int i=1;
    for (; NULL != ptr; i++)
    {
        if (strcmp(ptr->data.name, name)==0)
        {
            break;
        }
        ptr = ptr->next;
    }
delete_node(list,i);
    scanf_file(ptr->data.IDcard,list);
    return NULL;
}
// //单链表的清空
// void clear_node_list(u_list *list);
